package com.minivision.sdktestdemo.ui.custom;

import android.content.Context;

import com.minivision.sdktestdemo.utils.CloseUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

import rx.Observable;

/**
 * Created by Administrator on 2016/8/4.
 */
public class DiskLrcLoader implements LrcLoader {
    private Context mContext;
    private static final String LRC_DIR_NAME = "lrcs";
    private String mLrcDirName;
    private volatile String mMusicName;
    private LrcLoader mLoader;
    private String mErrorMsg;

    synchronized void  save(String name, String content) {
        File file = mContext.getDir(mLrcDirName, Context.MODE_PRIVATE);
        file = new File(file, name);
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseUtils.close(writer);
        }
    }

    public DiskLrcLoader(Context context) {
        this(context, LRC_DIR_NAME);
    }

    public DiskLrcLoader(Context context, String lrcDirName) {
        mLrcDirName = lrcDirName;
        mContext = context;
    }

    @Override
    public String getLrcText(String name) {
        mMusicName = name;
        String content = getLrcTextFromLocal(name);
        if (content.isEmpty()) {
            if (mLoader != null) {
                content = mLoader.getLrcText(name);
                if (content.isEmpty()) {
                    mErrorMsg = mLoader.getErrorMsg();
                } else {
                    if (mMusicName.equals(name)) {
                        save(name, content);
                    }
                }
            } else {
                mErrorMsg = "没有找到相关歌曲的歌词";
            }
        }
        return content;
    }

    @Override
    public String getErrorMsg() {
        return mErrorMsg;
    }

    synchronized String getLrcTextFromLocal(String name) {
        File file = mContext.getDir(mLrcDirName, Context.MODE_PRIVATE);
        file = new File(file, name);
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            reader = new BufferedReader(new FileReader(file));
            builder.setLength(0);
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append("\r\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseUtils.close(reader);
        }
        return builder.toString();
    }

    @Override
    public LrcLoader setLrcLoader(LrcLoader loader) {
        mLoader = loader;
        return this;
    }
}